#include "server.h"
#include "recvEntry.h"
#include "transfile.h"
#include "log.h"
void serverWork(int netfd){
    
    char* nowPath=(char *)calloc(4096,1);   
    MYSQL *mysql = mysql_init(NULL);
    MYSQL *ret = mysql_real_connect(mysql,
                                    "localhost",
                                    "root",
                                    "123456",
                                    "new",
                                    0,NULL,0);
    if(ret==NULL){
        fprintf(stderr,"mysql connect error:%s\n",mysql_error(mysql));
    }
    char* username = login(netfd,mysql);

    //NETDISK_LOG(username);
    Lstack stack;
    
    initializeStack(&stack);
    push(&stack,username);

    while(strcmp(username, "failed") == 0){
        username = login(netfd,mysql);
    }

    NETDISK_LOG(username);

    //命令实现区
    //char* toPath="?";//cyw     dir2
    //char* nowPath="?";//cyw    usr/dir1
    //char* realPath="?";//ljx
    //char* current_path="?";//lzc
    //char* filename="?";//lzc
    strcpy(nowPath, "pan/");
    strcat(nowPath,username);//nowPath=user;
    char* lusername=(char*)malloc(1024);
    strcpy(lusername,username);
    while(1){
    ENTRY entry;
    recvEntry(netfd,&entry);//
     switch(entry.CMD){
    case CD:
        cd(entry.data,nowPath,mysql);
        break;
    case LS:
        //lx
        lsServer(netfd,nowPath,mysql);
        break;
    case PUTS:
        //lzc
        putsFile(netfd,nowPath,entry.data,mysql);
        break;
    case GETS:
        //lzci
        getsFile(netfd,nowPath,entry.data,mysql);
        break;
    case REMOVE:
        //ljx
        reMove(nowPath,mysql,&entry);
        break;
    case PWD:
        //ljx
        mPwd(netfd,nowPath,mysql);
        break;
    case MKDIR:
        //ljx
        mkDir(nowPath,mysql,lusername,&entry);
        break;
    case RMDIR:
        //ljx
        rmDir(nowPath,mysql,&entry);
        break;
    case EXIT:
        break;
    }
}
}


